//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.platform.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.LiveExamClass;
import com.ruoyi.live.base.domain.SystemClass;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.LiveExamClassMapper;
import com.ruoyi.live.base.mapper.SystemClassMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.commons.IMD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class SystemClassService {
    private static final Logger log = LoggerFactory.getLogger(SystemClassService.class);
    @Autowired
    SystemClassMapper dao;
    @Autowired
    UserMapper userDao;
    @Autowired
    CacheService cacheService;
    @Autowired
    LiveExamClassMapper examClassDao;

    public SystemClassService() {
    }

    public void addSystemClass(SystemClass entity) throws Exception {
        try {
            entity.setCreateTime(System.currentTimeMillis());
            entity.setCreateUser(SessionInfo.getUserId());
            entity.setState(1);
            if (entity.getTeacherId() == null) {
                if (SessionInfo.getUser().getUserType() != 2) {
                    throw new Exception("");
                }

                entity.setTeacherId(SessionInfo.getUserId());
            }

            this.dao.insert(entity);
            if ("2".equals(this.cacheService.getParam("SYSTEM_PATTERN"))) {
                this.examClassDao.insert((new LiveExamClass()).setClassId(entity.getId()).setExamId(1L).setState(1));
            }

        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("新增SystemClass失败,编号SystemClassService-addSystemClass-1");
            throw new Exception("新增失败,编号SystemClassService-addSystemClass-1");
        }
    }

    public void updateSystemClass(SystemClass entity) throws Exception {
        try {
            this.dao.updateById(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("更新SystemClass失败,编号SystemClassService-updateSystemClass-1,id:{}", entity.getId());
            throw new Exception("更新失败,编号SystemClassService-updateSystemClass-1");
        }
    }

    public SystemClass getSystemClass(Long id) {
        return (SystemClass)this.dao.selectById(id);
    }

    public int deleteSystemClass(Long[] ids) {
        UpdateWrapper<SystemClass> updateWrapper = new UpdateWrapper<SystemClass>();
        updateWrapper.in("id", ids).set("state", 0);
        int count = this.dao.update(new SystemClass(), updateWrapper);
        return count;
    }

    public List<SystemClass> query(SystemClass entity) {
        return this.dao.selectClasses(entity.getClassName(), entity.getTeacherId());
    }

    public void createUsers(String name, Integer count, Integer userCount, Long userSchool) throws Exception {
        if (!ICommons.isNullOrEmpty(name) && !ICommons.isNullOrEmpty(count, true) && !ICommons.isNullOrEmpty(userCount, true)) {
            Long password = IMD5.getMD5(this.cacheService.getParam("SYSTEM_DEFAULT_PASSWORD"));

            for(int i = count; i <= userCount; ++i) {
                String userName = name + i;

                try {
                    this.userDao.insert((new User()).setClassId(0L).setUserPasswordMd5(password).setUserCreateTime(System.currentTimeMillis()).setUserCreateUserId(SessionInfo.getUserId()).setUserType(4).setUserLoginName(userName).setUserLoginNameMd5(IMD5.getMD5(userName)).setUserState(1).setUserSchool(userSchool).setUserRealName("专家" + i));
                } catch (Exception var9) {
                    var9.printStackTrace();
                }
            }

        } else {
            throw new Exception("");
        }
    }

    public static String getRandomText(Integer count) {
        Random random = new Random();
        String result = "";

        for(int i = 0; i < count; ++i) {
            result = result + random.nextInt(10);
        }

        return result;
    }
}
